﻿  
//动态创建json
function setJson(){
     var json={};
    var tab = document.getElementById('tab');
    var rows1 = tab.rows.length-1;
    var theme=document.getElementById('theme2');
    var key1=document.getElementById('key21');
    var relation=document.getElementById('relation2');
    var key2=document.getElementById('key22');
    var special=document.getElementById('special2');
    json.query={};
    json.query.booleanQuery=[];
    json.query.booleanQuery[0]={};
    json.query.booleanQuery[0].theme1=theme.options[theme.selectedIndex].value;
    json.query.booleanQuery[0].key1=key1.value;
    json.query.booleanQuery[0].relation=relation.options[relation.selectedIndex].text;
    json.query.booleanQuery[0].key2=key2.value;
    json.query.booleanQuery[0].special=special.options[special.selectedIndex].text;
    json.occur=[];
    json.occur[0]='and';
   
  if(rows1>2)
{
      for(var row=3;row<=rows1;row++)
   {
     var selRow=document.getElementById('selRow'+row+'1');
      theme=document.getElementById('theme'+row);
       key1=document.getElementById('key'+row+'1');
        relation=document.getElementById('relation'+row);
       key2=document.getElementById('key'+row+'2');
        special=document.getElementById('special'+row);
          json.occur[row-2]=selRow.options[selRow.selectedIndex].value;
          json.query.booleanQuery[row-2]={};
          json.query.booleanQuery[row-2].theme1=theme.options[theme.selectedIndex].value;
          json.query.booleanQuery[row-2].key1=key1.value;
          json.query.booleanQuery[row-2].relation=relation.options[relation.selectedIndex].text;
          json.query.booleanQuery[row-2].key2=key2.value;
          json.query.booleanQuery[row-2].special=special.options[special.selectedIndex].text;
     }
  }  


 var tab1 = document.getElementById('tab1');
  var rows2 = tab1.rows.length;
  var year1= document.getElementById('year21');
  var year2= document.getElementById('year22');
  json.query.RangeQuery={};
  json.query.RangeQuery.start=year1.options[year1.selectedIndex].text;
  json.query.RangeQuery.end=year2.options[year2.selectedIndex].text;
  json.occur[rows1-1]='and';

  json.query.fieldQuery={};
  json.query.fieldQuery.res={};
  json.query.fieldQuery.fund={};
   var res=document.getElementById('docRes');
   special=document.getElementById('special_3');
  json.query.fieldQuery.res.value=res.value;
  json.query.fieldQuery.res.special= special.options[special.selectedIndex].text;
  json.occur[rows1]='and';

var fund=document.getElementById('fund')
  special=document.getElementById('special_4');
  json.query.fieldQuery.fund.value=fund.value;
  json.query.fieldQuery.fund.special= special.options[special.selectedIndex].text;
   json.occur[rows1+1]='and';

  json.query.booleanQuery1=[];
    json.query.booleanQuery1[0]={};
    var authS=document.getElementById('authorS5');
    var author=document.getElementById('author5');
    special=document.getElementById('special_51');
    json.query.booleanQuery1[0].autT=authS.options[authS.selectedIndex].value;
    json.query.booleanQuery1[0].key1=author.value;
    json.query.booleanQuery1[0].special1=special.options[special.selectedIndex].text;

   var AU=document.getElementById('AU5');
    special=document.getElementById('special_52');
    json.query.booleanQuery1[0].key2=AU.value;
    json.query.booleanQuery1[0].special2=special.options[special.selectedIndex].text;
    json.occur[rows1+2]='and';


  var row3=rows1+3;
if(rows2>6){
  for(var row=6;row<=rows2-1;row++)
   {
    var relation=document.getElementById('selRow'+row+'2');
     json.occur[row3++]=relation.options[relation.selectedIndex].value;
     json.query.booleanQuery1[row-5]={};
     authS=document.getElementById('authorS'+row);
    
     author=document.getElementById('author'+row);
     special=document.getElementById('special_'+row+'1');
    json.query.booleanQuery1[row-5].autT=authS.options[authS.selectedIndex].value;
    json.query.booleanQuery1[row-5].key1=author.value;
    json.query.booleanQuery1[row-5].special1=special.options[special.selectedIndex].text;

    var AU=document.getElementById('AU'+row);
    special=document.getElementById('special_'+row+'2');
    json.query.booleanQuery1[row-5].key2=AU.value;
    json.query.booleanQuery1[row-5].special2=special.options[special.selectedIndex].text;
    } 
}
var str=JSON.stringify(json);
document.getElementById('totals').value=str; 
}
    //动态添加tab表中的行   
    function addRow(){   
         //添加一行
    var tbl = document.getElementById('tab');
    var lastRow = tbl.rows.length;
    
    // if there's no header row in the table, then iteration = lastRow + 1
    var iteration = lastRow;
if(lastRow<7)
{
    var row = tbl.insertRow(lastRow);
// select cell
var cellRightSel = row.insertCell(0);
var sel = document.createElement('select');
sel.name = 'selRow' + iteration+"1";
sel.id = 'selRow' + iteration+"1";
sel.options[0] = new Option('并且', 'and');
sel.options[1] = new Option('或者', 'or');
sel.options[2] = new Option('不含', 'not');
cellRightSel.appendChild(sel);
 
var cell01 = row.insertCell(1);
 var textNode=document.createTextNode('('); 
cell01.appendChild(textNode);

var cell02=row.insertCell(2);
sel = document.createElement('select');
sel.name = 'theme' + iteration;
sel.id = 'theme' + iteration;
var arr=new Array("主题","篇名","关键词","摘要","全文");
var values=new Array("content","title","keywords","summary","content");
for(var i=0;i<arr.length;i++)
{
  sel.options[i]=new Option(arr[i],i);
  sel.options[i].value=values[i];
}
cell01.appendChild(sel);

 cell02=row.insertCell(3);
 var input=document.createElement('input');
 input.name='key'+iteration+'1';
 input.id='key'+iteration+'1';
 cell02.appendChild(input);

 cellRightSel = row.insertCell(4);
 sel = document.createElement('select');
 sel.name = 'relation' + iteration;
 sel.id = 'relation' + iteration;
 sel.options[0] = new Option('并含', 'and');
 sel.options[1] = new Option('或含', 'or');
 sel.options[2] = new Option('不含', 'not');
 cellRightSel.appendChild(sel);

 cell02=row.insertCell(5);
 var input=document.createElement('input');
 input.name='key'+iteration+'2';
 input.id='key'+iteration+'2';
 input.type='text';
 cell02.appendChild(input);

  cell02=row.insertCell(6);
  sel = document.createElement('select');
  sel.name = 'special' + iteration;
  sel.id = 'special' + iteration;
  sel.options[0]=new Option('精确',0);
  sel.options[1]=new Option('模糊',1);
  cell02.appendChild(sel);

 cell01 = row.insertCell(7);
 var textNode=document.createTextNode(')'); 
 cell01.appendChild(textNode);
}
    }  
        
//动态删除tab表格的行
function deleteRow()
{
var lowNum;
 var num=document.getElementById("tab").rows.length;
 if(num>3)
 {
  lowNum=num-1;
  var tb=document.getElementById("tab");
  tb.deleteRow(lowNum);
 }
} 
 
//动态增加tab1中的行
function addRow1(){
    var tb2 = document.getElementById('tab1');
    var lastRow1 = tb2.rows.length-1;
    var iteration2 = lastRow1;
    var iteration1 = lastRow1+1;
if(lastRow1 <9){
 var row1 = tb2.insertRow(lastRow1);
// select cell
var cellRightSel1 = row1.insertCell(0);
var sel1 = document.createElement('select');
sel1.name = 'selRow' + iteration1+"2";
sel1.id = 'selRow' + iteration1+"2";
sel1.options[0] = new Option('并且', 'and');
sel1.options[1] = new Option('或者', 'or');
sel1.options[2] = new Option('不含', 'not');
cellRightSel1.appendChild(sel1);

cellRightSel1 = row1.insertCell(1);
  sel1 = document.createElement('select');
  sel1.name = 'authorS' + iteration1;
  sel1.id = 'authorS' + iteration1;
  sel1.options[0] = new Option('作者', 'author');
  sel1.options[1] = new Option('第一作者', 'author');
  cellRightSel1.appendChild(sel1);

var cell02=row1.insertCell(2);
 var input=document.createElement('input');
 input.type='text';
 input.name = 'author' + iteration1;
 input.id = 'author' + iteration1;
 cell02.appendChild(input);

cellRightSel1 = row1.insertCell(3);
 sel1 = document.createElement('select');
sel1.name = 'special_' + iteration1+'1';
sel1.id = 'special_' + iteration1+'1';
sel1.options[0] = new Option('精确', '精确');
sel1.options[1] = new Option('模糊', '模糊');
cellRightSel1.appendChild(sel1);

var cell01 = row1.insertCell(4);
 var textNode=document.createTextNode('作者单位:'); 
cell01.appendChild(textNode);

 cell02=row1.insertCell(5);
 var input=document.createElement('input');
 input.type='text';
 input.name = 'AU' + iteration1;
 input.id = 'AU' + iteration1;
 cell02.appendChild(input)

cellRightSel1 = row1.insertCell(6);
 sel1 = document.createElement('select');
sel1.name = 'special_' + iteration1+'2';
sel1.id = 'special_' + iteration1+'2';
sel1.options[0] = new Option('精确', '精确');
sel1.options[1] = new Option('模糊', '模糊');
cellRightSel1.appendChild(sel1);
}
} 

//动态删除tab1表格的行
function deleteRow1()
{
 var lowNum;
 var num=document.getElementById("tab1").rows.length-1;
 if(num>5)
 {
  lowNum=num-1;
  var tb=document.getElementById("tab1");
  tb.deleteRow(lowNum);
 }
}  